#include <iostream>
#include <string>
using namespace std;

long long translateToDecimal(string s,long long radix){
	long long power = 1;
	long long result = 0;
	for(int i=s.length()-1; i>-1; i--){
		if( isdigit(s[i]) ) result += (s[i]-'0')*power;
		else{
			result += (s[i]-'a'+10)*power;
		}
		power *= radix;
	}
	return result;
}
int maxValueOf(string s){
	int max = 0;
	for(int i=0; i<s.length(); i++){
		if( s[i]>max ) max = s[i];
	}
	return isdigit(max) ? max-'0':max-'a'+10;
}
long long radixOf(string dest,string src,long long radix ){
	long long s = translateToDecimal(src,radix),d = -1;
	long long i=maxValueOf(dest)+1,j=max(s,i);
	while( i<=j ){
        long long m = (i+j) / 2;
		if( (d=translateToDecimal(dest,m))==s ){
            return m;
		}else if( d>s || d<0 ){
            j = m - 1;
        }else{
            i = m + 1;
        }
	}
	return 1;
}
int main(){
	string a,b;
	int flag,radix;
	cin >> a >> b >> flag >> radix;
	long long res;
	if( flag == 1 ){
		if( (res=radixOf(b,a,radix)) != 1 ) cout << res;
		else cout << "Impossible"; 
	}else{
		if( (res=radixOf(a,b,radix)) != 1 ) cout << res;
		else cout << "Impossible";
	}
	return 0;
}
